import Vue from 'vue';
//import state from '../../store/state.js';
//import { appVm } from '../../main.js';
//请求成功
function handleSuccess(res) {
    return res;
}

//请求失败
function handleError(err) {
    Vue.$vux.loading.hide();	
	if(err.status == 404){
		Vue.$vux.toast.show({
            type:'cancel',
            position:'middle',
            text: '服务未找到',
            isShowMask:true,
            onHide(){
				// if (state.bridge.callHandler) {
				// 	state.bridge.callHandler('backNative');
				// }                
            }
        });		
	}else if(err.status == 500 || err.status == 502){
        Vue.$vux.toast.show({
            type:'cancel' ,
            position:'middle' ,
            text: '服务出现问题' ,
            isShowMask:true,
            onHide(){
				// if (state.bridge.callHandler) {
				// 	state.bridge.callHandler('backNative');
				// }                
            }
        });		
    }
    return err
}

// get请求
function getAjax(type, url, params) {
    return Vue.http[type](
        url, { params ,timeout:10000}
    ).then(res => handleSuccess(res), err => handleError(err))
}

// post请求
function postAjax(type, url, body, params ,headers) {
    return Vue.http[type](
        url,
        body, { params ,timeout:10000}
    ).then(res => handleSuccess(res), err => handleError(err))
}
//基础请求方法
function baseAjax(type, url, body, params) {
    switch (type) {
        case 'get':
        case 'delete':
            return getAjax(type, url, params)
        case 'post':
        case 'put':
            return postAjax(type, url, body, params)
    }

}

// 输出get请求
function hget(url, params) {
    return baseAjax('get', url, {}, params)
        .then(res => res)
}
// 输出loading框get
function hgetl(url, params) {

    return baseAjax('get', url, {}, params)
        .then(res => res)
}
//delete
function hdelete(url, body, params) {
    // if(!params) params = body;
    return baseAjax('delete', url, body, params)
}

function hdeletel(url, body, params) {

    // if(!params) params = body;
    return baseAjax('delete', url, body, params)
}
// 特殊的get请求，
function hgets(url, params) {
    let query = '';
    for (let key in params) {
        query += `/${key}/${params[key]}`;
    }
    return Vue.http.get(
        url + query, {}
    ).then(res => handleSuccess(res), err => handleError(err))
}

function hgetsl(url, params) {
    let query = '';
    for (let key in params) {
        query += `/${key}/${params[key]}`;
    }

    return Vue.http.get(
        url + query, {}
    ).then(res => handleSuccess(res), err => handleError(err))
}

function hgetr(url, params) {
    let query = '';
    for (let key in params) {
        query += `/${params[key]}`;
    }
    return Vue.http.get(
        url + query, {}
    ).then(res => handleSuccess(res), err => handleError(err))
}

function hgetrl(url, params) {
    let query = '';
    for (let key in params) {
        query += `/${params[key]}`;
    }

    return Vue.http.get(
        url + query, {}
    ).then(res => handleSuccess(res), err => handleError(err))
}

// 输出post请求
function hpost(url, body, params) {
    // if(!params) params = body;
    return baseAjax('post', url, body, params)
}

function hpostl(url, body, params) {

    // if(!params) params = body;
    return baseAjax('post', url, body, params)
}


//输出put请求
function hput(url, body, params) {
    return baseAjax('put', url, body, params)
}

function hputl(url, body, params) {

    return baseAjax('put', url, body, params)
}

function hputs(url, body, params) {
    let query = '';
    for (let key in params) {
        query += `/${key}/${params[key]}`;
    }
    return Vue.http.put(url + query, body).then(res => handleSuccess(res), err => handleError(err))
}

function hputr(url, body, params) {
    let query = '';
    for (let key in params) {
        query += `/${params[key]}`;
    }
    return Vue.http.put(url + query, body).then(res => handleSuccess(res), err => handleError(err))
}

function hputrl(url, body, params) {
    let query = '';
    for (let key in params) {
        query += `/${params[key]}`;
    }

    return Vue.http.put(url + query, body).then(res => handleSuccess(res), err => handleError(err))
}

function hpostr(url, body, params) {
    let query = '';
    for (let key in params) {
        query += `/${params[key]}`;
    }

    return Vue.http.post(url + query, body).then(res => handleSuccess(res), err => handleError(err))
}

function hpostrl(url, body, params) {
    let query = '';
    for (let key in params) {
        query += `/${params[key]}`;
    }

    return Vue.http.post(url + query, body).then(res => handleSuccess(res), err => handleError(err))
}


function exportLoad(url, params) {
    let query = '';
    for (let key in params) {
        query += `${key}=${params[key]}&`;
    }
    window.location.href = url + '?' + query
}

// 输出delete请求
function hdelete(url, body, params) {
    // if(!params) params = body;
    return baseAjax('delete', url, {}, params)
}

function hdeletel(url, body, params) {

    // if(!params) params = body;
    return baseAjax('delete', url, {}, params)
}

function hdeleter(url, params) {
    let query = '';
    for (let key in params) {
        query += `/${params[key]}`;
    }
    return Vue.http.delete(
        url + query, {}
    ).then(res => handleSuccess(res), err => handleError(err))
}

function hdeleterl(url, params) {
    let query = '';
    for (let key in params) {
        query += `/${params[key]}`;
    }

    return Vue.http.delete(
        url + query, {}
    ).then(res => handleSuccess(res), err => handleError(err))
}

function hgetn(url, params) {
    let query = '?';
    for (let i in params) {
        query += i + "=" + params[i] + "&"
    }
    return Vue.http.get(
        url + query, {}
    ).then(res => handleSuccess(res), err => handleError(err))
}
export default {
    get: hget,
    getl: hgetl,
    post: hpost,
    postl: hpostl,
    put: hput,
    puts: hputs,
    putl: hputl,
    gets: hgets,
    getsl: hgetsl,
    getr: hgetr,
    getrl: hgetrl,
    putr: hputr,
    putrl: hputrl,
    postr: hpostr,
    postrl: hpostrl,
    delete: hdelete,
    deletel: hdeletel,
    deleter: hdeleter,
    deleterl: hdeleterl,
    export: exportLoad,
    getn: hgetn

}